// pages/index/index.js
let app = getApp();
let http = require('../../utils/fetch.js')
var api = require('../../utils/api.js')

// isLogin 0为未注册，1未待审核，2为审核
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: 0,
    imgUrls: [
      '../../images/01.jpg',
      '../../images/02.jpg',
      '../../images/03.jpg'
    ],
    audioUrl: 'https://smartcity.honghengxinxi.com/public/dd.mp3',
    indicatorDots: false,
    selectComm: '',//选中的小区
    selectCID: '',
    autoplay: true,
    interval: 60000,
    duration: 1000,
    gateList: [
      // { plotName: '碧峰园', city: '宜宾市', area: '翠屏区' },
      // { plotName: '碧峰园', city: '宜宾市', area: '翠屏区' },
    ],
    idx: 0,
    deviceListNum: []
  },
  // 事件处理函数、
  //打开透明层
  showRule: function () {
    this.setData({
      isRuleTrue: true
    })
  },
  //关闭透明层
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  // 申请开门权限
  apply: function () {
    wx.navigateTo({
      url: '../permission/permission',
    })
  },
  login: function () {
    wx.navigateTo({
      // url: '../login/username/username?source=index',
      url: '../permission/permission',
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.onLoad();
  },
  requestRight: function () {
    wx.navigateTo({
      url: '../permission/permission',
    })
    this.setData({
      isRuleTrue: false
    })
  },
  selectPlot: function (e) {
    this.setData({
      selectComm: e.currentTarget.dataset.item.community[0].name,
      selectCID: e.currentTarget.dataset.item.community[0].CID,
      isRuleTrue: false
    })
    this.getDeviceList(e.currentTarget.dataset.item.community[0].CID)
  },
  scroll(e) {
  },
  // audioPlay(){

  //   this.audioCtx.play()
  // },
  toOpenDevice(e) {

    let params = {
      LDID: e.currentTarget.dataset.ldid
    }

    http.post(api.openDevice, params).then((res) => {
      if (res.code == 0) {
        //  this.setData({
        //   deviceListNum:res.data
        //  })
        this.audioCtx.play()
        wx.showToast({
          title: "开门成功",
          icon: "none"
        })
    


      }
    })
      .catch((errMsg) => {
        //错误提示信息
        wx.showToast({
          title: "开门失败",
          icon: "none"
        })
      });
  },
  //小区列表
  getCommunity() {
    http.post(api.getComList).then((res) => {
      console.log(res)
      if (res.code == 0) {
        this.setData({
          gateList: res.data,
          selectComm: res.data[0].name
        })
      }
    })
      .catch((errMsg) => {
        //错误提示信息
        wx.showToast({
          title: errMsg,
          icon: "none"
        })
      });
  },
  //设备列表
  getDeviceList(CID) {
    let params = {
      CID: CID
    }
    http.post(api.deviceList, params).then((res) => {
   
      if (res.code == 0) {

        this.setData({
          deviceListNum: res.data
        })
     

      }
    })
      .catch((errMsg) => {
        //错误提示信息
        wx.showToast({
          title: errMsg,
          icon: "none"
        })
      });
  },


  // 微信登陆
  getOpenid: function () {
    let that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data:{
              appid: 'wxd21c3257f68ffeae',
              secret: '54ff5076c4a0d0223db4b0377cc73443',
              js_code: res.code,
              grant_type: 'authorization_code'
            },
            method:"GET",
            success:function(res){
              if(res.data.openid){
                http.post(api.getlogin,{openid:res.data.openid}).then(
                  data=>{
                    wx.setStorage({
                      data: data.token,
                      key: 'token',
                    })
                    if(data.data)  app.globalData.weixin = data.data ;
                    that.setData({
                      isLogin:data.data.status ||　0
                    })
                  }
                )
              }
            }
           
          })
        }
      }
    })
  },
  

  // 


/**
 * 生命周期函数--监听页面加载
 */
onLoad: function (options) {
  wx.showToast({ icon: "loading", duration: 1000 })
  wx.stopPullDownRefresh({
    success: (res) => { },
  })
  var that = this;
  that.getOpenid()
  that.getCommunity()
  that.getDeviceList()
  let token = wx.getStorageSync('token')
  // if (token) {
  //   that.setData({
  //     isLogin: 1
  //   })
  // } else {
  //   that.setData({
  //     isLogin: 0
  //   })
  // }  
},

/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady: function () {
  this.audioCtx = wx.createAudioContext('myAudio')


},

/**
 * 生命周期函数--监听页面显示
 */
onShow: function () {

},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide: function () {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload: function () {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh: function () {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom: function () {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage: function () {

}
})